package com.aliang.shopping.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.aliang.shopping.common.PageResult;
import com.aliang.shopping.common.ResultCodeEnum;
import com.aliang.shopping.contact.SqlColumnConstant;
import com.aliang.shopping.exception.DataException;
import com.aliang.shopping.mapper.AdminMapper;
import com.aliang.shopping.mapper.BusinessMapper;
import com.aliang.shopping.mapper.LoginLogMapper;
import com.aliang.shopping.mapper.UserInfoMapper;
import com.aliang.shopping.model.converter.CommonConverter;
import com.aliang.shopping.model.converter.LoginLogConverter;
import com.aliang.shopping.model.dto.common.PageDTO;
import com.aliang.shopping.model.enums.UserTypeEnum;
import com.aliang.shopping.model.po.Admin;
import com.aliang.shopping.model.po.Business;
import com.aliang.shopping.model.po.LoginLog;
import com.aliang.shopping.model.po.UserInfo;
import com.aliang.shopping.model.vo.loginLog.LoginLogExportVO;
import com.aliang.shopping.model.vo.loginLog.LoginLogVO;
import com.aliang.shopping.security.auth.UserHolder;
import com.aliang.shopping.service.LoginLogService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ShiBaLover
 * @description 针对表【login_log(登录日志表)】的数据库操作Service实现
 * @createDate 2024-10-17 19:05:38
 */
@Service
@AllArgsConstructor
public class LoginLogServiceImpl extends ServiceImpl<LoginLogMapper, LoginLog>
        implements LoginLogService {
    private final CommonConverter<LoginLogVO> commonConverter = new CommonConverter<>();

    private final LoginLogConverter loginLogConverter;

    @Override
    public PageResult<LoginLogVO> selectPage(PageDTO pageDTO) {
        //查询数据
        IPage<LoginLogVO> page = new Page<>(pageDTO.getPageNum(), pageDTO.getPageSize());
        IPage<LoginLogVO> mapperPage = this.baseMapper.getPage(page, pageDTO);
        //转换数据
        return commonConverter.toPageResult(mapperPage);
    }

    private final UserInfoMapper userInfoMapper;
    private final BusinessMapper businessMapper;
    private final AdminMapper adminMapper;
    @Override
    public List<LoginLogExportVO> getExportList(PageDTO pageDTO) {
        List<LoginLogVO> list = this.baseMapper.getVoList(pageDTO);
        if (list.isEmpty()) {
            throw new DataException(ResultCodeEnum.NOT_QUERY_RESULT);
        }
        //转换数据
        return loginLogConverter.toImportDTOList(list);
    }

    public List<LoginLogVO> getLoginLogByKeyword() {
        // 获取当前登录用户的信息
        Integer userRole = UserHolder.getUserRole();
        Long userId = UserHolder.getUserId();
        // 获取数据
        Map<String, String> map = new HashMap<>();
        //根据角色去查阅用户信息
        if (ObjectUtil.equal(UserTypeEnum.USER.getCode(), userRole)) {
            //用户角色
            UserInfo info = this.userInfoMapper.selectById(userId);
            //获取用户名、邮箱、电话等信息
            map.put(SqlColumnConstant.USERNAME, info.getUsername());
            map.put(SqlColumnConstant.EMAIL, info.getEmail());
            map.put(SqlColumnConstant.PHONE, info.getPhone());
        } else
        if (ObjectUtil.equal(UserTypeEnum.BUSINESS.getCode(), userRole)) {
            //用户角色
            Business info = this.businessMapper.selectById(userId);
            //获取用户名、邮箱、电话等信息
            map.put(SqlColumnConstant.USERNAME, info.getUsername());
            map.put(SqlColumnConstant.EMAIL, info.getEmail());
            map.put(SqlColumnConstant.PHONE, info.getPhone());
        } else {
            //用户角色
            Admin info = this.adminMapper.selectById(userId);
            //获取用户名、邮箱、电话等信息
            map.put(SqlColumnConstant.USERNAME, info.getUsername());
            map.put(SqlColumnConstant.EMAIL, info.getEmail());
            map.put(SqlColumnConstant.PHONE, info.getPhone());
        }
        //拼接查询语句
        LambdaQueryWrapper<LoginLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LoginLog::getKeyword, map.get(SqlColumnConstant.USERNAME))
                .or()
                .eq(LoginLog::getKeyword, map.get(SqlColumnConstant.EMAIL))
                .or()
                .eq(LoginLog::getKeyword, map.get(SqlColumnConstant.PHONE))
                //根据创建时间倒序
                .orderByDesc(LoginLog::getCreateTime)
                // 只获取最新的10条
                .last("limit 10");
        //查询数据
        List<LoginLog> list = this.baseMapper.selectList(wrapper);
        //转换数据并返回
        return loginLogConverter.toVOList(list);
    }
}




